package com.my.mytest.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.my.mytest.R;
import com.my.mytest.util.GlideRoundTransform;
import com.my.mytest.util.LogUtil;
import com.my.mytest.util.MViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Android Studio.
 * User: mwb
 * Date: 2020/12/11 0011
 * Time: 下午 2:13
 * Describe:
 */
public class LayoutAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<Integer> data;
    int size[] = new int[2];

    public LayoutAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
    }

    public void setData(List<Integer> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    private static final int TYPE_HEADER = 1, TYPE_ITEM = 2;


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE_HEADER:
                view = LayoutInflater.from(context).inflate(R.layout.item_header, parent, false);
                return new HeaderViewHolder(view);

            case TYPE_ITEM:
                view = LayoutInflater.from(context).inflate(R.layout.item_card_view, parent, false);
                return new SimpleViewHolder(view);

        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case TYPE_HEADER:
                if (holder instanceof HeaderViewHolder) {
                    StaggeredGridLayoutManager.LayoutParams clp = (StaggeredGridLayoutManager.LayoutParams) ((HeaderViewHolder) holder).rl_header.getLayoutParams();
                    clp.setFullSpan(true);

                    ViewGroup.LayoutParams lp = ((HeaderViewHolder) holder).rl_header.getLayoutParams();
                    lp.width = context.getResources().getDisplayMetrics().widthPixels;
                    lp.height = MViewUtil.dp2px(context, 200);

                    ((HeaderViewHolder) holder).rl_header.setLayoutParams(lp);
                }

                break;
            case TYPE_ITEM:
                if (holder instanceof SimpleViewHolder) {
                    final ViewGroup.LayoutParams lp1 = ((SimpleViewHolder) holder).item.getLayoutParams();

                    size = getImageRealSize(data.get(position - 1));
                    lp1.width = context.getResources().getDisplayMetrics().widthPixels / 2;
                    lp1.height = context.getResources().getDisplayMetrics().widthPixels / 2 * size[1] / size[0];
                    ((SimpleViewHolder) holder).item.setLayoutParams(lp1);

                    ((SimpleViewHolder) holder).tv_position.setText("第" + position + "个");

                    Glide.with(context).
                            load(data.get(position - 1))
                            .transform(new CenterCrop(), new GlideRoundTransform(context, 10))
                            .diskCacheStrategy(DiskCacheStrategy.ALL) // 缓存策略
                            .into(((SimpleViewHolder) holder).item);
                }
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position = position == 0 ? TYPE_HEADER : TYPE_ITEM;
    }

    @Override
    public int getItemCount() {
        return data.size() + 1;
    }

    private class SimpleViewHolder extends RecyclerView.ViewHolder {
        ImageView item;
        TextView tv_position;

        public SimpleViewHolder(View view) {
            super(view);
            item = view.findViewById(R.id.item_card_iv);
            tv_position = view.findViewById(R.id.tv_position);
        }
    }

    private class HeaderViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rl_header;

        public HeaderViewHolder(@NonNull View itemView) {
            super(itemView);
            rl_header = itemView.findViewById(R.id.rl_header);
        }
    }

    private int[] getImageRealSize(int id) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        /**
         * 最关键在此，把options.inJustDecodeBounds = true;
         * 这里再decodeFile()，返回的bitmap为空，但此时调用options.outHeight时，已经包含了图片的高了
         */
        options.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id, options);
        int size[] = new int[2];
        size[0] = options.outWidth;
        size[1] = options.outHeight;

//        Glide.with(context)
//                .asBitmap()
//                .load(id)
//                .into(new SimpleTarget<Bitmap>() {
//                    @Override
//                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
//
//                    }
//                });
//
//
//        Glide.with(context)
//                .asBitmap()
//                .load(id)
//                .into(new CustomTarget<Bitmap>() {
//                    @Override
//                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
//                        int height = resource.getHeight();
//                        int width = resource.getWidth();
//                    }
//
//                    @Override
//                    public void onLoadCleared(@Nullable Drawable placeholder) {
//                    }
//                });
//
        return size;
    }
}
